from xml.etree.cElementTree import *

class uniGameobject:
    def __init__(self):
        #rozmiar kafelka obrazka
        self.tilew=32
        self.tileh=32
        #id objektu
        self.id=0
        #nazwa objektu
        self.name="noname"
        #opis objektu
        self.description="none"
        #type np. container weapon notype environment
        self.type="notype"
        #atk ilosc zadawanych obrazen
        self.atk=0
        #armor obrona przedmiotu 9999 niezniszczalny
        self.armor=9999
        #ilosc klatek dla animacji
        self.nrframes=1
        #polozenie obiektu na mapie
        self.poz_x=0
        self.poz_y=0
        self.poz_z=0
        #priorytet do rysowania 
        self.draw_priority=0

    #load object from file
    #id must be string etc 0
    def loadobject(self,filename,id):
        #game object file structure
        #
        #<gameobject>
        #   <name>drzewo</name>
        #   <description>male drzewko</description>
        #   <tile w="32" h="32" nrframes="1" draw_priority="0"/>
        #   <type>environment</type>
        #   <stats atk="0" armor="9999"/>
        #   <position x="0" y="0" z="0"/>
        #/<gameobject>
        doc=parse(filename)
        root=doc.getroot()
        #in all <gameobject> in tree
        for e in root:
            #in all nodes in e
            for i in e:
                #is searching node?
                if i.text==id :
                    for s in e:
                        if s.tag=="id":self.id=int(s.text)
                        if s.tag=="name":self.name=s.text
                        if s.tag=="description":self.description=s.text
                        if s.tag=="tile":
                            self.tilew=int(s.attrib["w"])
                            self.tileh=int(s.attrib["h"])
                            self.nrframes=int(s.attrib["nrframes"])
                            self.draw_priority=int(s.attrib["draw_priority"])
                        if s.tag=="type":self.type==s.text
                        if s.tag=="stats":
                            self.atk=int(s.attrib["atk"])
                            self.armor=int(s.attrib["armor"])
                        if s.tag=="position":
                            self.poz_x=int(s.attrib["x"])
                            self.poz_y=int(s.attrib["y"])
                            self.poz_z=int(s.attrib["z"])
    
    
    #save this object to file
    def saveobject(self,filename):
        doc=parse(filename)
        root=doc.getroot()
        gobj=Element("gameobject")
        #id
        elem=Element("id")
        elem.text=str(self.id)
        gobj.append(elem)
        #name
        elem=Element("name")
        elem.text=self.name
        gobj.append(elem)
        #description
        elem=Element("description")
        elem.text=self.description
        gobj.append(elem)
        #
        elem=Element("tile")
        elem.attrib["w"]=str(self.tilew)
        elem.attrib["h"]=str(self.tileh)
        elem.attrib["nrframes"]=str(self.nrframes)
        elem.attrib["draw_priority"]=str(self.draw_priority)
        gobj.append(elem)
        #type
        elem=Element("type")
        elem.text=self.type
        gobj.append(elem)
        #stats
        elem=Element("stats")
        elem.attrib["atk"]=str(self.atk)
        elem.attrib["armor"]=str(self.armor)
        gobj.append(elem)
        #position
        elem=Element("position")
        elem.attrib["x"]=str(self.poz_x)
        elem.attrib["y"]=str(self.poz_y)
        elem.attrib["z"]=str(self.poz_z)
        gobj.append(elem)
        #add game object to database
        root.append(gobj)
        self.indent(root)
        doc.write(open(filename,"w"))
        
    #preety saving function for elementtree
    def indent(self,elem, level=0):
        i = "\n" + level*"  "
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + "  "
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
            for elem in elem:
                self.indent(elem, level+1)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        else:
            if level and (not elem.tail or not elem.tail.strip()):
                elem.tail = i
